﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

namespace eGrid_App
{
    /// <summary>
    /// Interaction logic for BookmarkInfoTip.xaml
    /// </summary>
    public partial class BookmarkInfoTip : ScatterViewItem
    {
        ScatterLayer parentLayer = null;
        BookmarkPin ttp;

        #region constructors
        public BookmarkInfoTip(ScatterLayer parent, BookmarkPin pin)
        {
            InitializeComponent();

            parentLayer = parent;

            ttp = pin;
            TT_Num.Text = pin.Bookmark.ID;
            TT_SEV.Text = pin.Bookmark.getBookmarkPriorityString();

            /* Change the background color of the Priority text accordingly */
            switch (TT_SEV.Text)
            {
                case "High":
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Red);
                    SS_SEV.Value = 1;
                    break;
                case "Medium":
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Yellow);
                    SS_SEV.Value = 2;
                    break;
                case "Low":
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Green);
                    SS_SEV.Value = 3;
                    break;
                default:
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Gray);
                    break;
            }

            TT_DES.Text = pin.Description;
            TT_LOC.Text = pin.Bookmark.MapCoordinate.ToString();

            ShowMapButton.Visibility = Visibility.Visible;
        }

        public BookmarkInfoTip(BookmarkPin pin)
        {

            InitializeComponent();

            ttp = pin;
            TT_Num.Text = pin.Bookmark.ID;
            TT_SEV.Text = pin.Bookmark.getBookmarkPriorityString();
            TT_DES.Text = pin.Description;
            TT_LOC.Text = pin.Bookmark.MapCoordinate.ToString();
        }
        #endregion

        #region event handlers

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            if (EditButton.Content.Equals("Edit"))
            {
                SwitchToEditMode();
                return;
            }

            if (EditButton.Content.Equals("Done Edit"))
            {
                SwitchToViewMode();
                return;
            }
        }

        private void ShowMapButton_Click(object sender, RoutedEventArgs e)
        {
            OpenMapFrame();
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            HideWindow();
        }

        private void SS_SEV_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (e.OldValue == 0.0) return;

            int newValue = (int)e.NewValue;
            ChangePriority(newValue);
        }
        #endregion


        #region utility functions
        
        private void SwitchToEditMode()
        {
            TT_DES.IsEnabled = true;
            SS_SEV.Visibility = Visibility.Visible;
            EditButton.Background = new SolidColorBrush(Colors.Red);
            EditButton.Content = "Done Edit";
            /* Cannot exit when editing */
            CloseButton.IsEnabled = false;
            CloseButton.Background = new SolidColorBrush(Colors.Gray);
            UpdateLayout();
        }
        
        private void SwitchToViewMode()
        {
            TT_DES.IsEnabled = false;
            SS_SEV.Visibility = Visibility.Collapsed;
            EditButton.Background = new SolidColorBrush(Colors.Aqua);
            EditButton.Content = "Edit";
            /* Re-enable the close button to allow the user to close the MapTip */
            CloseButton.IsEnabled = true;
            CloseButton.Background = new SolidColorBrush(Colors.White);
            /* Update the trouble ticket */
            ttp.Bookmark.Description = TT_DES.Text;
            UpdateLayout();
        }

        private void OpenMapFrame()
        {
            this.Visibility = Visibility.Hidden;

            if (parentLayer == null) return;

            // FIX ME: the orientation and position of the map frame
            parentLayer.CreateMapFrameFromBookmarkPin(ttp.Bookmark, Center, Orientation);
        }

        private void HideWindow()
        {
            this.Visibility = Visibility.Hidden;
        }

        private void ChangePriority(int newValue)
        {
            switch (newValue)
            {
                case 1:
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Red);
                    TT_SEV.Text = "High";
                    ttp.Priority = Bookmark.BookmarkPriority.High;
                    break;
                case 2:
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Yellow);
                    TT_SEV.Text = "Medium";
                    ttp.Priority = Bookmark.BookmarkPriority.Medium;
                    break;
                case 3:
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Green);
                    TT_SEV.Text = "Low";
                    ttp.Priority = Bookmark.BookmarkPriority.Low;
                    break;
                default:
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Gray);
                    TT_SEV.Text = "N/A";
                    break;
            }
        }

        #endregion
    }
}
